package com.neusoft.actor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.neusoft.actor.dao.CaretakerClientDao;
import com.neusoft.actor.dao.MessageDao;
import com.neusoft.actor.po.CaretakerClient;
import com.neusoft.actor.po.Message;
import com.neusoft.actor.po.ResponseBean;
import com.neusoft.actor.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageDao messageDao;

    @Autowired
    private CaretakerClientDao caretakerClientDao;

    @Override
    public void saveMessage(Message message) {
        // 查询护工ID
        CaretakerClient caretakerClient = caretakerClientDao.selectOne(new QueryWrapper<CaretakerClient>().eq("client_id", message.getClientId()));
        if (caretakerClient != null) {
            message.setNurseId(caretakerClient.getCaretakerId());
        } else {
            throw new RuntimeException("未找到对应的护工信息");
        }
        // 设置消息时间为当前时间
        message.setMessageTime(new Date());
        // 保存消息
        messageDao.insert(message);
    }

    @Override
    public ResponseBean<List<Message>> getMessagesByCareTakerId(Integer careTakerId) {
        return new ResponseBean<>(messageDao.selectList(new QueryWrapper<Message>().eq("nurse_id", careTakerId)));
    }

    @Override
    public ResponseBean<Integer> emergency(Message message) {
        CaretakerClient caretakerClient = caretakerClientDao.selectOne(new QueryWrapper<CaretakerClient>().eq("client_id", message.getClientId()));
        if (caretakerClient != null) {
            message.setNurseId(caretakerClient.getCaretakerId());
        } else {
            message.setNurseId(1);
        };
        message.setMessageTime(new Date());
        message.setMessageContent(message.getClientName() + "紧急求助！！！");
        return new ResponseBean<Integer>(messageDao.insert(message));
    }
}
